<template>
	<view class="container">
		<view class="content">
			<view class="title">
				<view>登录</view>
			</view>
			<view class="wrap">
				<!-- <view class="small_title">手机号码</view> -->
				<input type="text" placeholder="请输入手机号码" v-model="form.phone" style="font-size: 32rpx;">
			</view>
			<view class="wrap code">
				<!-- <view class="small_title">手机号码</view> -->
				<input type="text" placeholder="输入图形验证码" v-model="imgCodeContent" style="font-size: 32rpx;">
				<image class="img_code" @click="getCode()" :src="imageCodeData.imageData" mode=""></image>
			</view>
			<view class="wrap code">
				<!-- <view class="small_title">密码</view> -->
				<input type="text" placeholder="请输入验证码" v-model="form.code" style="font-size: 32rpx;">
				<view class="code_btn" @click="sendCode" v-if="sendYes">发送验证码</view>
				<view class="code_btn" v-else>发送{{times}}S</view>
			</view>
			<view class="button" @click="getEnterprice">立即登录</view>
			<view class="gologin">
				<!-- <view>还没帐号？<text @click="$gourl('/pages/index/register',3)">前往注册</text></view> -->
			</view>
		</view>
		<u-popup v-model="maskShow" mode="center" border-radius="14">
			<view class="mask_content">
				<view class="mask_title">
					请选择企业
				</view>
				<view class="qy_box">
					<view class="qy_lb" :class="current == index?'active':''" v-for="(item,index) in enterpriceList"
						@click="current = index">
						{{item.name}}
					</view>
				</view>
				<view class="buttons" @click="onsubmit">确定</view>
			</view>
		</u-popup>
		<u-select v-model="show" :list="list"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: '',
					code: '',
					enterprise_id: ''
				},
				current: 0,
				maskShow: false,
				imageCodeData: null,
				imgCodeContent: '',
				times: 59,
				sendYes: true,
				enterpriceList: '',
			}
		},
		onLoad() {
			this.getCode()
		},
		methods: {
			getCode() {
				this.$allApi.getImgCode({
					type: 'login',
					company_id: 1
				}).then(res => {
					console.log(res);
					this.imageCodeData = res.data
				})
			},
			sendCode() {
				if (this.form.phone == '') {
					this.$u.toast('手机号码不能为空')
					return
				} else if (this.imgCodeContent == '') {
					this.$u.toast('图形验证码不能为空')
					return
				}
				this.$allApi.sendCode({
					mobile: this.form.phone,
					type: 'login',
					token: this.imageCodeData.imageToken,
					yzm: this.imgCodeContent
				}).then(res => {
					console.log(res);
					this.sendYes = false
					let timer = setInterval(() => {
						console.log(this.times--);
					}, 1000)
					setTimeout(() => {
						clearInterval(timer)
						this.sendYes = true
						this.times = 59
					}, 5000)
				})
			},
			getEnterprice() {
				this.maskShow = true
				this.$allApi.getEnterprisesList({
					type: 'mobile',
					// value: this.form.phone,
					value: '17320487668',
				}).then(res => {
					console.log(res);
					this.maskShow = true
					this.enterpriceList = res.data
				})
			},
			onsubmit() {
				this.$allApi.login({
					auth_type: 'local',
					check_type: 'mobile',
					username: this.form.phone,
					vcode: this.form.code,
					enterprise_id: this.enterpriceList[this.current].id
				}).then(res => {
					this.$u.toast('登陆成功')
					uni.setStorageSync('userToken', res.data.token)
					uni.setStorageSync('logonData', JSON.stringify(res.data))
					setTimeout(() => {
						this.$gourl('/pages/index/index', 2)
					}, 1000)
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.mask_content {
		width: 600rpx;
		padding: 30rpx;

		.mask_title {
			font-size: 32rpx;
			font-weight: bold;
		}

		.qy_box {
			.qy_lb {
				padding: 20rpx;
				margin: 20rpx 0;
				border: 1px solid #eee;
				border-radius: 20rpx;
			}
		}

		.active {
			color: #d33124;
			border: 1px solid #d33124 !important;
		}

		.buttons {
			width: 300rpx;
			margin: 0 auto;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background: #d33124;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-top: 30rpx;
		}
	}



	.code_btn {
		width: 260rpx;
		height: 86rpx;
		color: #d33124;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #d33124;
		margin-left: 30rpx;
		border-radius: 43rpx;
	}

	.img_code {
		width: 260rpx;
		height: 86rpx;
		margin-left: 30rpx;
	}

	.code {
		display: flex;
		align-items: center;
		justify-content: space-between;


	}

	.container {
		min-height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
		// background: linear-gradient(#d33124, #f7f7f7);
	}

	.content {
		background: #fff;
		width: 690rpx;
		border-radius: 10rpx;
		padding: 60rpx;
		position: relative;
		// box-shadow: 0 0 10px 5px #ddd;
		z-index: 999;

		.title {
			font-size: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #d33124;
			margin-bottom: 100rpx;

		}

		.wrap {
			margin-top: 30rpx;

			.small_title {
				font-size: 32rpx;
				padding-top: 30rpx;
				color: #666;
			}

			input {
				padding: 20rpx 30rpx;
				border-radius: 10rpx;
				background: #f7f7f7;
			}
		}

		.button {
			padding: 20rpx 30rpx;
			background: #d33124;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 460rpx;
			border-radius: 10px;
		}

		.gologin {
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 30rpx 0;
			width: 630rpx;
			color: #666;
			bottom: -98rpx;
			left: 30rpx;
			// box-shadow:
			// 	inset 0 5px 5px #ddd;
			border-radius: 2px 0 10rpx 10rpx;
			background: #fff;
			z-index: 1;

			text {
				color: #000;
				font-weight: bold;
			}
		}
	}
</style>
